package com.easy.easylibdemo.ui.listedit;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.easy.easylibdemo.R;
import com.guyj.CommonAdapter;
import com.guyj.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ListEditActivity extends AppCompatActivity {

//    @Bind(R.id.list)
//    ListView list;
    @Bind(R.id.list)
    RecyclerView recyclerView;
    int pos=-1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_edit);
        ButterKnife.bind(this);
        List<String> list=new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("123");
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(ListEditActivity.this,LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(new CommonAdapter<String>(ListEditActivity.this,R.layout.item_edit,list) {
            SparseArray<Boolean> watchNeedCreateAry=new SparseArray();
            SparseArray<String> etTextAry=new SparseArray();
            SparseArray<TextWatcher> textWatcherAry=new SparseArray();
            TextWatcher textWatcher;
            @Override
            protected void convert(ViewHolder viewHolder, String s, final int i) {
                Log.e("tag", i + "convert==");
                viewHolder.setText(R.id.tv_name,i+"");
                viewHolder.setText(R.id.et_text,etTextAry.get(i));
                if (pos == i) {
                    viewHolder.getView(R.id.et_text).requestFocus();
                    ((EditText) viewHolder.getView(R.id.et_text)).setSelection(((EditText) viewHolder.getView(R.id.et_text)).getText().length());
                }
//                if (watchNeedCreateAry.get(i,true)){
//                Log.e("tag", i + "new TextWatcher==");
                    textWatcher = new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                            Log.e("tag", i + "beforeTextChanged==" + s);
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
//                            Log.e("tag",i+ "onTextChanged==" + s);
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            Log.e("tag",i+ "afterTextChanged==" + s );
                            etTextAry.put(i,s.toString());
                        }
                    };
                    ((EditText) viewHolder.getView(R.id.et_text)).addTextChangedListener(textWatcher);
                    textWatcherAry.put(i,textWatcher);
//                    watchNeedCreateAry.put(i,false);
//                }

                    viewHolder.setOnTouchListener(R.id.et_text, new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if (event.getAction() == MotionEvent.ACTION_UP) {
//                            etPos = position;
                                pos = i;
                            }
                            return false;
                        }
                    });
            }

            @Override
            public void onViewDetachedFromWindow(ViewHolder holder) {
                super.onViewDetachedFromWindow(holder);
                Log.e("tag","onViewDetachedFromWindow=getLayoutPosition=" + holder.getLayoutPosition() );
                ((EditText) holder.getView(R.id.et_text)).removeTextChangedListener(textWatcherAry.get(holder.getLayoutPosition()));
                textWatcherAry.remove(holder.getLayoutPosition());
            }
        });


//        list.setAdapter(new BaseAdapter() {
//
//            LayoutInflater inflate=LayoutInflater.from(ListEditActivity.this);
//            ViewHolder viewHolder;
//            EditText mET;
//            private int etPos=-1;
//            @Override
//            public int getCount() {
//                return 20;
//            }
//
//            @Override
//            public Object getItem(int position) {
//                return position;
//            }
//
//            @Override
//            public long getItemId(int position) {
//                return position;
//            }
//
//            @Override
//            public View getView(final int position, View convertView, ViewGroup parent) {
//                Log.e("tag","11111111");
//                if (convertView==null){
//                    Log.e("tag","222222222");
//                    convertView= inflate.inflate(R.layout.item_edit,null);
//                    viewHolder=new ViewHolder();
//                    viewHolder.et= (EditText) convertView.findViewById(R.id.et_text);
//                    viewHolder.tv= (TextView) convertView.findViewById(R.id.tv_name);
//                    viewHolder.cb= (CheckBox) convertView.findViewById(R.id.cb_text);
//                    convertView.setTag(viewHolder);
//                }else{
//                    Log.e("tag","33333333");
//                    viewHolder=(ViewHolder)convertView.getTag();
//                }
//                mET=viewHolder.et;
//
//                if (position==etPos){
//                    Log.e("tag","444444444444");
//                    mET.requestFocus();
////                    viewHolder.et.setSelection(viewHolder.et.getText().length());
//                }else{
//                    mET.clearFocus();
//                }
//                mET.setOnTouchListener(new View.OnTouchListener() {
//                    @Override
//                    public boolean onTouch(View v, MotionEvent event) {
//                        if (event.getAction() == MotionEvent.ACTION_UP) {
//                            etPos = position;
//                        }
//                        return false;
//                    }
//                });
//
//
//                return convertView;
//            }
//
//            class ViewHolder{
//                EditText et;
//                TextView tv;
//                CheckBox cb;
//            }
//        });
    }
}
